let emailRegex = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
let token;

$(function () {
    //从cookie中获取访问令牌
    token = $.cookie("token");
    if(token == null || token == "") {
        //为空回到登录页面
        location.href = "login.html"
        return;
    }
    //异步请求用户信息
    let userData;
    $.ajax({
        url: "user_info",
        type:'post',
        contentType : "application/json",
        data: JSON.stringify({token: token}),
        dataType:'json',
        success:function(value){
            if(value.success) {
                //console.log(JSON.stringify(value.data));
                userData = value.data;
                showUserData(userData);
            } else {
                $.messager.alert("提示" ,value.data, "info");
                if(value.data == "访问令牌无效") {
                	//回到登录页面
                	location.href = "login.html";
                    return;
                }
            }
        }
    });

    //选择图片以后显示在img标签上
    $("#browseImage").change(function(){
        $("#headPortrait").attr("src", URL.createObjectURL($(this)[0].files[0]));
    });
});

//向表单标签上设置值
function showUserData(userData) {

    //设置用户名
    $("#username").val(getData(userData.username));

    //设置手机号
    $("#phone").val(getData(userData.phone));

    //设置邮箱
    $("#email").val(getData(userData.email));

    //设置注册时间
    let registerDate = getData(userData.registerDate);
    if(registerDate != "-") {
        registerDate = new Date(userData.registerDate).Format("yyyy-MM-dd hh:mm:ss");
        $("#registerDate").val(registerDate);
    }
    $("#registerDate").val(registerDate);

    //设置状态
    let state = userData.state;
    if(state == 0) {
        state = "正常";
    } else if(state == 1) {
        state = "封禁";
    } else {
        state = "-";
    }
    $("#state").val(state);

    //设置头像
    let headPortraitUrl = getData(userData.headPortraitUrl);
    if(headPortraitUrl != "-") {
        $("#headPortrait").attr("src", headPortraitUrl);
    }
}

//编辑用户信息
function userEdit() {
    //隐藏不可修改的一些内容
    $(".editHiden").hide();
    //显示一些信息
    $(".showHiden").show();
    $("#imageAdd").show();
    //设置可编辑
    $("#username").attr("disabled", false);
    $("#email").attr("disabled", false);
}

//展示用户信息
function userShow() {
    //隐藏不可修改的一些内容
    $(".editHiden").show();
    //显示一些信息
    $(".showHiden").hide();
    $("#imageAdd").hide();
    //设置可编辑
    $("#username").attr("disabled", true);
    $("#email").attr("disabled", true);
}

//选择图片
function imageChange() {
    let imagePath = $("#browseImage").val();
    alert(imagePath);
    $("#headPortrait").attr("src", URL.createObjectURL(imagePath));
}

//提交表单
function submitData() {
    let username = $("#username").val();
    let email = $("#email").val();
    if(username == null || username == "") {
        $.messager.alert("提示", "用户名不能为空!", "info");
        return false;
    }
    if(email == null || email == "") {
        $.messager.alert("提示", "邮箱地址不能不能为空!", "info");
        return false;
    }
    if(!emailRegex.test(email)) {
        $.messager.alert("提示", "请输入正确格式的邮箱地址!", "info");
        return false;
    }
    $("#token").val(token);
    //异步提交表单并获取返回数据
    $("#userForm").ajaxSubmit(function (value) {
        let jsonObject = JSON.parse(value);
        if(jsonObject.success) {
            $.messager.alert("提示" ,"更改成功!", "info");
            setTimeout(function () {
                location.reload();
            }, 500);
        } else {
            $.messager.alert("提示" ,jsonObject.data, "info");
        }
    });
    return false;
}

//退出登录
function logout() {
    //删除cookie
    $.removeCookie("token");
    //转至登录页面
    location.href = "login.html";
}

//获取格式话的值
function getData(data) {
    if(data != null && data != "" && data != " ") {
        return data;
    } else {
        return "-";
    }
}